﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;

namespace Cms.EntLib
{
    public class CookiesUtility
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cookieName">Cookie名称</param>
        /// <returns>TRUE 为空</returns>
        public static bool checkCookie(string cookieName)
        {
            if (object.Equals(System.Web.HttpContext.Current.Request.Cookies[cookieName], null))
                return true;
            else
                return false;
        }

        public static void deleteCookie(string cookieName)
        {
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookieName];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now;
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
        public static void deleteAllCookie()
        {
            string[] allCookie = System.Web.HttpContext.Current.Request.Cookies.AllKeys;
            if (allCookie != null && allCookie.Length > 0)
            {
                foreach (string c in allCookie)
                {
                    if (!String.IsNullOrEmpty(c))
                        deleteCookie(c);
                }
            }
        }

        public static string getCookie(string cookieName)
        {
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookieName];
            if (cookie != null)
                return HttpUtility.UrlDecode(cookie.Value);
            else
                return null;
        }

        public static void setCookie(string cookieName, string cookieValue, int days)
        {
            cookieValue = HttpUtility.UrlEncode(cookieValue);
            HttpCookie cookie = new HttpCookie(cookieName, cookieValue);
            cookie.Expires = DateTime.Now.AddDays(days);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
    }
}
